home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 12.0 KB | 525 lines | [TEXT/CWIE] |
- // PickMeshShapePart.c
- //
- // This demonstrates how to use picks to test which parts of a mesh geometry have
- // been hit. When the mesh is picked the fPickParts value set from the ShapeParts
- // menu controls the parts tested: object, face, edge, or vertex. As the returned
- // hits in the pick are processed the corresponding part of the mesh is draw in a
- // different color and the name of that part is displayed in the window's upper
- // left corner.
- //
- // The vertex and edge tolerance levels set in CreatePick() control how close the
- // window point pick must be to the given part.
- //
- // Robert Dierkes - November 11, 1995
- //
- // ©1994-95 Apple computer Inc., All Rights Reserved
- //
-
- // system headers
- #include <Windows.h>
-
- // for QuickDraw 3D
- #include "QD3D.h"
- #include "QD3DMath.h"
- #include "QD3DDrawContext.h"
- #include "QD3DShader.h"
- #include "QD3DGroup.h"
- #include "QD3DPick.h"
-
-
- #include "PickMeshShapePart.h"
-
-
- //-------------------------------------------------------------------------------------------
- // function prototypes
-
- void TickDelay(
- long time);
-
- TQ3Status DoGroupPicking(
- TQ3ViewObject view,
- TQ3GroupObject group,
- TQ3PickObject pick);
-
- void GetRandomRGBColor(
- TQ3ColorRGB *pColorRGB);
-
- TQ3Status ChangeMeshFaceColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshFace meshFace,
- DocumentPtr theDocument);
-
- TQ3Status ChangeMeshEdgeColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshEdge meshEdge,
- DocumentPtr theDocument);
-
- TQ3Status ChangeMeshVertexColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshVertex meshVertex,
- DocumentPtr theDocument);
-
- TQ3Status HandleMeshPickHits(
- TQ3PickObject pick,
- DocumentPtr theDocument);
-
- TQ3Status CreatePick(
- TQ3PickObject *pPick);
-
-
- //-------------------------------------------------------------------------------------------
- //
-
- #define kDelay 30
- #define kTextLocationX 30
- #define kTextLocationY 30
-
- static TQ3PickObject gPick = NULL;
-
-
-
- //-------------------------------------------------------------------------------------------
-
- TQ3Status InitPicking(
- void)
- {
- return CreatePick(&gPick);
- }
-
-
- TQ3Status ExitPicking(
- void)
- {
- if (gPick != NULL) {
- Q3Object_Dispose(gPick);
- gPick = NULL;
- }
-
- return kQ3Success;
- }
-
-
- static
- void TickDelay(
- long time)
- {
- long ticks,
- startTicks;
-
- startTicks = ticks = TickCount();
-
- while (ticks - startTicks != time ) {
- ticks = TickCount();
- }
- }
-
-
- static
- TQ3Status DoGroupPicking(
- TQ3ViewObject view,
- TQ3GroupObject group,
- TQ3PickObject pick)
- {
- TQ3Status status;
- TQ3ViewStatus viewStatus;
-
- if ((status = Q3View_StartPicking(view, pick)) == kQ3Failure)
- return status;
-
- do {
- status = Q3DisplayGroup_Submit(group, view);
-
- viewStatus = Q3View_EndPicking(view);
- } while (viewStatus == kQ3ViewStatusRetraverse);
-
- if (viewStatus != kQ3ViewStatusDone) {
- status = kQ3Failure;
- }
-
- return status;
- }
-
-
- static
- void GetRandomRGBColor(
- TQ3ColorRGB *pColorRGB)
- {
- unsigned long ticks;
-
- ticks = TickCount();
- Q3ColorRGB_Set(pColorRGB,
- (ticks % 10) / 10.0,
- (ticks % 100) / 100.0,
- (ticks % 1000) / 1000.0);
- }
-
-
- static
- TQ3Status ChangeMeshFaceColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshFace meshFace,
- DocumentPtr theDocument)
- {
- TQ3Status status;
- TQ3AttributeSet attributeSet;
- TQ3ColorRGB colorRGB;
-
- status = kQ3Failure;
-
- if (meshGeo == NULL ||
- meshFace == NULL) {
- return status;
- }
-
- /* Get the face attributes */
- if ((status = Q3Mesh_GetFaceAttributeSet(meshGeo, meshFace, &attributeSet)) == kQ3Failure) {
- return status;
- }
-
- /* Create an attributes if none */
- if (attributeSet == NULL) {
- if ((attributeSet = Q3AttributeSet_New()) == NULL) {
- return kQ3Failure;
- }
- }
-
- GetRandomRGBColor(&colorRGB);
- if ((status = Q3AttributeSet_Add(attributeSet, kQ3AttributeTypeDiffuseColor, &colorRGB)) == kQ3Success) {
-
- char partTypeString[] = {"\pFace"};
-
- /* Set new face color */
- status = Q3Mesh_SetFaceAttributeSet(meshGeo, meshFace, attributeSet);
-
- /* Redraw the mesh */
- DrawDocumentData(theDocument);
- MoveTo(kTextLocationX, kTextLocationY);
- DrawText((void *) partTypeString, 1, (short) partTypeString[0]);
- TickDelay(kDelay);
-
- /* Remove new face color */
- status = Q3Mesh_SetFaceAttributeSet(meshGeo, meshFace, NULL);
- DrawDocumentData(theDocument);
- }
-
- Q3Object_Dispose(attributeSet);
-
- return status;
- }
-
-
- static
- TQ3Status ChangeMeshEdgeColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshEdge meshEdge,
- DocumentPtr theDocument)
- {
- TQ3Status status;
- TQ3AttributeSet attributeSet1,
- attributeSet2;
- TQ3MeshVertex meshVertex1,
- meshVertex2;
- TQ3ColorRGB colorRGB;
-
- status = kQ3Failure;
-
- if (meshGeo == NULL ||
- meshEdge == NULL) {
- return status;
- }
-
- attributeSet1 = NULL;
- attributeSet2 = NULL;
-
- /*
- * Set color on both vertices of this edge
- *
- * We change the vertex attributes instead of the edge attributes because we can only
- * see the effects of an edge attribute when the fill style is kQ3FillStyleEdges. We
- * could use kQ3FillStyleEdges rather than the current fill of kQ3FillStyleFilled but
- * then we couldn't see the attributes on the vertex and face.
- */
- if ((status = Q3Mesh_GetEdgeVertices(meshGeo, meshEdge, &meshVertex1, &meshVertex2)) == kQ3Failure) {
- return status;
- }
-
- /* Get the vertex attributes */
- if (((status = Q3Mesh_GetVertexAttributeSet(meshGeo, meshVertex1, &attributeSet1)) == kQ3Failure) ||
- ((status = Q3Mesh_GetVertexAttributeSet(meshGeo, meshVertex2, &attributeSet2)) == kQ3Failure)) {
- goto ExitChangeMeshEdgeColor;
- }
-
- /* Create an attributes if none */
- if (attributeSet1 == NULL) {
- if ((attributeSet1 = Q3AttributeSet_New()) == NULL) {
- goto ExitChangeMeshEdgeColor;
- }
- }
-
- if (attributeSet2 == NULL) {
- if ((attributeSet2 = Q3AttributeSet_New()) == NULL) {
- goto ExitChangeMeshEdgeColor;
- }
- }
-
- GetRandomRGBColor(&colorRGB);
- if (((status = Q3AttributeSet_Add(attributeSet1, kQ3AttributeTypeDiffuseColor, &colorRGB)) == kQ3Success) &&
- ((status = Q3AttributeSet_Add(attributeSet2, kQ3AttributeTypeDiffuseColor, &colorRGB)) == kQ3Success)) {
-
- char partTypeString[] = {"\pEdge"};
-
- /* Set new vertex color */
- if (((status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex1, attributeSet1)) == kQ3Success) &&
- ((status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex2, attributeSet2)) == kQ3Success)) {
-
- /* Redraw the mesh */
- DrawDocumentData(theDocument);
- MoveTo(kTextLocationX, kTextLocationY);
- DrawText((void *) partTypeString, 1, (short) partTypeString[0]);
- TickDelay(kDelay);
- }
-
- /* Remove new vertex color */
- status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex1, NULL);
- status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex2, NULL);
- DrawDocumentData(theDocument);
- }
-
- ExitChangeMeshEdgeColor:
-
- if (attributeSet1 != NULL)
- Q3Object_Dispose(attributeSet1);
-
- if (attributeSet2 != NULL)
- Q3Object_Dispose(attributeSet2);
-
- return status;
- }
-
-
- static
- TQ3Status ChangeMeshVertexColor(
- TQ3GeometryObject meshGeo,
- TQ3MeshVertex meshVertex,
- DocumentPtr theDocument)
- {
- TQ3Status status;
- TQ3AttributeSet attributeSet;
- TQ3ColorRGB colorRGB;
-
- status = kQ3Failure;
-
- if (meshGeo == NULL ||
- meshVertex == NULL) {
- return status;
- }
-
- /* Get the vertex attributes */
- if ((status = Q3Mesh_GetVertexAttributeSet(meshGeo, meshVertex, &attributeSet)) == kQ3Failure) {
- return status;
- }
-
- /* Create an attributes if none */
- if (attributeSet == NULL) {
- if ((attributeSet = Q3AttributeSet_New()) == NULL) {
- return kQ3Failure;
- }
- }
-
- GetRandomRGBColor(&colorRGB);
- if ((status = Q3AttributeSet_Add(attributeSet, kQ3AttributeTypeDiffuseColor, &colorRGB)) == kQ3Success) {
-
- char partTypeString[] = {"\pVertex"};
-
- /* Set new vertex color */
- status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex, attributeSet);
-
- /* Redraw the mesh */
- DrawDocumentData(theDocument);
- MoveTo(kTextLocationX, kTextLocationY);
- DrawText((void *) partTypeString, 1, (short) partTypeString[0]);
- TickDelay(kDelay);
-
- /* Remove new vertex color */
- status = Q3Mesh_SetVertexAttributeSet(meshGeo, meshVertex, NULL);
- DrawDocumentData(theDocument);
- }
-
- Q3Object_Dispose(attributeSet);
-
- return status;
- }
-
-
- static
- TQ3Status HandleMeshPickHits(
- TQ3PickObject pick,
- DocumentPtr theDocument)
- {
- TQ3HitData hitData;
- TQ3ObjectType objectType;
- unsigned long hitIndex,
- numHits;
- TQ3Status status;
-
- status = kQ3Failure;
- numHits = 0;
-
- if (pick == NULL)
- goto ExitHandleMeshPickHits;
-
- if ((status = Q3Pick_GetNumHits(pick, &numHits)) == kQ3Failure)
- goto ExitHandleMeshPickHits;
-
- /* Get each hit */
- for (hitIndex = 0; hitIndex < numHits; hitIndex++) {
-
- if (Q3Pick_GetHitData(pick, hitIndex, &hitData) == kQ3Failure) {
- DebugStr("\pHandleMeshPickHits: Q3Pick_GetHitData failed");
- goto ExitHandleMeshPickHits;
- }
-
- /* Test if hit data contains an object and shape part */
- if (! HitData_Has_Object(hitData)) {
- DebugStr("\pHandleMeshPickHits: no object information returned in Q3Pick_GetHitData");
- goto ExitHandleMeshPickHits;
- }
-
- if (! HitData_Has_ShapePart(hitData)) {
- DebugStr("\pHandleMeshPickHits: no shape part information returned in Q3Pick_GetHitData");
- goto ExitHandleMeshPickHits;
- }
-
- /* Was the shapePart a mesh part? */
- if (Q3ShapePart_GetType (hitData.shapePart) == kQ3ShapePartTypeMeshPart) {
- TQ3GeometryObject meshGeo;
-
- /* hitData.object was tested for NULL by HitData_Has_Object() above */
- meshGeo = hitData.object;
-
- /* Or use this to get the shape part hit: objectType = Q3Object_GetLeafType(hitData.shapePart); */
- objectType = Q3MeshPart_GetType(hitData.shapePart);
-
- switch (objectType) {
- case kQ3MeshPartTypeMeshFacePart:
- {
- TQ3MeshFace meshFace;
-
- if (Q3MeshFacePart_GetFace(hitData.shapePart, &meshFace) == kQ3Failure) {
- goto ExitHandleMeshPickHits;
- }
-
- status = ChangeMeshFaceColor(meshGeo, meshFace, theDocument);
- }
- break;
-
- case kQ3MeshPartTypeMeshEdgePart:
- {
- TQ3MeshEdge meshEdge;
-
- if (Q3MeshEdgePart_GetEdge(hitData.shapePart, &meshEdge) == kQ3Failure) {
- goto ExitHandleMeshPickHits;
- }
-
- status = ChangeMeshEdgeColor(meshGeo, meshEdge, theDocument);
- }
- break;
-
- case kQ3MeshPartTypeMeshVertexPart:
- {
- TQ3MeshVertex meshVertex;
-
- if (Q3MeshVertexPart_GetVertex(hitData.shapePart, &meshVertex) == kQ3Failure) {
- goto ExitHandleMeshPickHits;
- }
-
- status = ChangeMeshVertexColor(meshGeo, meshVertex, theDocument);
- }
- break;
-
- default:
- break;
- }
- }
-
- Q3Hit_EmptyData(&hitData);
- }
-
- ExitHandleMeshPickHits:
-
- if (numHits > 0) {
- Q3Hit_EmptyData(&hitData);
- }
-
- if ((status = Q3Pick_EmptyHitList(pick)) == kQ3Failure)
- DebugStr("\pHandleMeshPickHits: Q3Pick_EmptyHitList failed");
-
- return status;
- }
-
-
- static
- TQ3Status CreatePick(
- TQ3PickObject *pPick)
- {
- TQ3WindowPointPickData data;
- TQ3Status status;
-
- status = kQ3Failure;
-
- if (pPick == NULL)
- return status;
-
- /* Create pick object */
- data.data.sort = kQ3PickSortNearToFar;
- data.data.mask = kQ3PickDetailMaskObject | /* Request only object and shape parts */
- kQ3PickDetailMaskShapePart;
- data.data.numHitsToReturn = kQ3ReturnAllHits;
-
- Q3Point2D_Set(&data.point, 0.0, 0.0);
- data.vertexTolerance = 5.0; /* Set vertex and edge tolerences */
- data.edgeTolerance = 3.0;
-
- if ((*pPick = Q3WindowPointPick_New(&data)) != NULL)
- status = kQ3Success;
-
- return status;
- }
-
-
- TQ3Status DoPicking(
- Point *pWhere, /* local window coordinates */
- DocumentPtr theDocument)
- {
- TQ3Status status;
- TQ3Point2D point;
-
- status = kQ3Failure;
-
- if (theDocument->fView == NULL ||
- theDocument->fModel == NULL ||
- theDocument->fPickPartStyle == NULL ||
- gPick == NULL) {
-
- goto DoPickingExit;
- }
-
- /* Set pick part style with parts selected in the menu */
- if ((status = Q3PickPartsStyle_Set(theDocument->fPickPartStyle, theDocument->fPickParts)) == kQ3Failure)
- goto DoPickingExit;
-
- /* Set the pick's point to mouse location */
- Q3Point2D_Set(&point, pWhere->h, pWhere->v);
- if ((status = Q3WindowPointPick_SetPoint(gPick, &point)) == kQ3Failure)
- goto DoPickingExit;
-
- if ((status = DoGroupPicking(theDocument->fView, theDocument->fModel, gPick)) == kQ3Failure)
- goto DoPickingExit;
-
- /* Handle hit(s) on mesh */
- status = HandleMeshPickHits(gPick, theDocument);
-
- DoPickingExit:
- return status;
- }
-